<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 *
 * 老师管理
 */
class Teacher extends MY_Controller {
  public $left_menu_min;

  function __construct(){
  parent::__construct();

    $parent_id = 12;
    //获取school二级菜单
    $this->load->model('School_manage_menu_model');
      $menu_list = $this->School_manage_menu_model->get_menu_list($this->is_manage,$parent_id);
      // print_r($menu_list);exit;
      $this->smarty->assign("menu_list", $menu_list);

      $this->left_menu_min = array("menu" => "基础设置", "curr" => '/teacher', "list" => $menu_list);
      $this->smarty->assign("left_menu_min", $this->left_menu_min);
      $this->smarty->assign("menu", 'setting');

  }
  
  /***
  **  学校老师管理
  */
  function index(){
    //获取老师
    $this->load->model('Teachers_model');
    $teachers = $this->Teachers_model->get_teacher_by_school(0,10,$this->user_data['school_id']);
    $this->smarty->assign("teachers", $teachers['result']);

    $this->smarty->display("teacher/teacher.html");
  }

  /**
  * 添加老师
  */
  public function teacher_add($value='')
  {
    //post data
    $post_data = $this->input->post();

    if($post_data){
      $has_error = 0;
      if(empty($post_data['teacher_name'])){
        $this->smarty->assign("teacher_name_err", "老师姓名不能为空");
        $has_error = 1;
      }else{
        $this->smarty->assign("teacher_name", $post_data['teacher_name']);
      }
      if(empty($post_data['mobile'])){
        $this->smarty->assign("mobile_err", "请填写老师手机号");
        $has_error = 1;
      }else{
        $this->smarty->assign("mobile", $post_data['mobile']);
      }
      if(empty($post_data['password'])){
        $this->smarty->assign("password_err", "请填写登录密码");
        $has_error = 1;
      }else{
        $this->smarty->assign("password", $post_data['password']);
      }

      if(!empty($post_data['header_img'])){
        $post_data['header_img'] = get_qiniu_url($post_data['header_img']);
      }

      //添加老师
      if($has_error==0){
        $this->load->model('Teachers_model');
        $teacher_id = $this->Teachers_model->add_teacher($this->user_data['school_id'],$post_data);
        if($teacher_id >0){
          header("Location:/teacher");exit;
        }
        else{
          // print_r($teacher_id);exit;
          $this->smarty->assign("mobile_err", $teacher_id);
          $has_error = 1;
        }
      }

      // print_r($post_data['img_url'][0]);
      // print_r(json_encode($post_data['img_url']));exit;
    }


    $this->smarty->display("teacher/teacher_add.html");
  }

  /**
   * 删除老师
   * @param  [type] $teacher_id [description]
   * @return [type]             [description]
   */
  public function del($teacher_id)
  {
     $this->load->model('Teachers_model');
    $teacher = $this->Teachers_model->get_teacher_by_id($teacher_id);

    if(empty($teacher)||$teacher['school_id']!=$this->user_data['school_id']){
      header("Location:/teacher");exit;
    }
    $this->Teachers_model->delete($teacher_id);
    header("Location:/teacher");exit;
  }

  /**
   * 重置登录密码
   * @param  [type] $teacher_id [description]
   * @return [type]             [description]
   */
  public function change_pwd($teacher_id)
  {
    $this->load->model('Teachers_model');
    $this->smarty->assign("teacher_id", $teacher_id);

    $post_data = $this->input->post();
    if($post_data){
        $has_error = 0;

        if(empty($post_data['password'])){
          $this->smarty->assign("password_err", "请填写新密码");
          $has_error = 1;
        }
        if(empty($post_data['password2'])){
          $this->smarty->assign("password2_err", "确认新密码");
          $has_error = 1;
        }
        if($has_error==0){
          if($post_data['password']!=$post_data['password2']){
            $this->smarty->assign("password_err", "二次密码不一致");
          }
        }
        
      

      //添加老师
      if($has_error==0){
        
        $result = $this->Teachers_model->change_pwd($teacher_id,$post_data['password']);
        if($result >0){
          header("Location:/teacher");exit;
        }
      }
    }

    $teacher = $this->Teachers_model->get_teacher_by_id($teacher_id);

    if(empty($teacher)||$teacher['school_id']!=$this->user_data['school_id']){
      header("Location:/teacher");exit;
    }

  
    $this->smarty->display("teacher/change_pwd.html");
  }

  /**
   * 调整评分
   * @param  [type] $teacher_id [description]
   * @return [type]             [description]
   */
  public function change_score($teacher_id, $limit = 0)
  {
    $this->load->model('Teachers_model');
    $this->smarty->assign("teacher_id", $teacher_id);

    $post_data = $this->input->post();
    if($post_data){
        $has_error = 0;

        if(empty($post_data['school_score'])){
          $this->smarty->assign("school_score_err", "请填写评分");
          $has_error = 1;
        }else{
          $this->smarty->assign("school_score", $post_data['school_score']);
        }
      

      //添加老师
      if($has_error==0){
        
        $result = $this->Teachers_model->change_score($teacher_id,$post_data['school_score']);
        if($result >0){
          header("Location:/teacher");exit;
        }
      }
    }

    $teacher = $this->Teachers_model->get_teacher_by_id($teacher_id);

    if(empty($teacher)||$teacher['school_id']!=$this->user_data['school_id']){
      header("Location:/teacher");exit;
    }

    $this->load->model('Teachers_model');
    $result = $this->Teachers_model->get_teacher_score_list($teacher_id, $limit, $this->offset);
    $this->load->library('pagination');
    $page_config['base_url'] = '/teacher/change_score/' . $teacher_id;
    $page_config['total_rows'] = $result['total_count'];
    $page_config['per_page'] = $this->offset;
    $page_config['uri_segment'] = 4;
    $this->pagination->initialize($page_config);
    $pagination = $this->pagination->create_links();
    $this->smarty->assign("pagination", $pagination);

    $this->smarty->assign("score_list", $result);

    $this->smarty->assign("school_score", $teacher['school_score']);
  
    $this->smarty->display("teacher/change_score.html");
  }

  /**
   * 编辑
   * @param  [type] $id [description]
   * @return [type]     [description]
   */
  function edit($id){
    $this->load->model('Teachers_model');
    $this->smarty->assign("teacher_id", $id);
    $post_data = $this->input->post();
    if($post_data){
        $has_error = 0;
        if(empty($post_data['teacher_name'])){
          $this->smarty->assign("teacher_name_err", "老师姓名不能为空");
          $has_error = 1;
        }else{
          $this->smarty->assign("teacher_name", $post_data['teacher_name']);
        }
      
        if(!empty($post_data['header_img'])){
          $post_data['header_img'] = get_qiniu_url($post_data['header_img']);
        }
      //添加老师
        
      if($has_error==0){
        $this->load->model('Teachers_model');
        $teacher_id = $this->Teachers_model->edit_teacher($id,$post_data);
        if($teacher_id >0){
          header("Location:/teacher");exit;
        }
      }
    }

    $teacher = $this->Teachers_model->get_teacher_by_id($id);

    if(empty($teacher)||$teacher['school_id']!=$this->user_data['school_id']){
      header("Location:/teacher");exit;
    }

    $this->smarty->assign("teacher_name", $teacher['teacher_name']);
    $this->smarty->assign("header_img", $teacher['header_img']);
    $this->smarty->assign("sex", $teacher['sex']);
    $this->smarty->assign("birthday", $teacher['birthday']);
    $this->smarty->assign("speciality", $teacher['speciality']);
    $this->smarty->assign("intro", $teacher['intro']);
    $this->smarty->assign("status", $teacher['status']);
    $this->smarty->assign("school_score", $teacher['school_score']);
    $this->smarty->assign("allow_live", $teacher['allow_live']);
    // $this->smarty->assign("", $teacher['']);
    $this->smarty->display("teacher/teacher_edit.html");
  }


  /**
   * 检查手机号
   * @param  [type] $phone [description]
   * @return [type]        [description]
   */
  public function check_phone($phone)
  {
    //判断手机号是否存在
    $this->load->model('User_model');
    $user = $this->User_model->get_user_by_phone_or_username($phone);
    if(empty($user)){
      echo "{'code':0}";//可用
    }else{
      if($user['is_teacher']==1){
        echo "{'code':9}";//不可用
      }else{
        echo "{'code':0}";//可用
      }
      
    }
    exit;
  }


}
